home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1O0WHQZ (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.9 KB  |  138 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.ButtonModel;
  5. import com.sun.java.swing.Icon;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.LookAndFeel;
  8. import com.sun.java.swing.SwingUtilities;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.plaf.ComponentUI;
  11. import com.sun.java.swing.plaf.ToggleButtonUI;
  12. import java.awt.Component;
  13. import java.awt.Container;
  14. import java.awt.Dimension;
  15. import java.awt.Font;
  16. import java.awt.FontMetrics;
  17. import java.awt.Graphics;
  18. import java.awt.Insets;
  19. import java.awt.Rectangle;
  20. import java.io.Serializable;
  21.  
  22. public class BasicRadioButtonUI extends BasicToggleButtonUI implements Serializable {
  23.    protected static final Insets defaultMargin = new Insets(2, 2, 2, 2);
  24.    protected Icon icon = null;
  25.    protected static ToggleButtonUI radioButtonUI;
  26.    ButtonModel model;
  27.  
  28.    public Icon createIcon() {
  29.       return UIManager.getIcon("RadioButton.icon");
  30.    }
  31.  
  32.    public static ComponentUI createUI(JComponent b) {
  33.       if (radioButtonUI == null) {
  34.          radioButtonUI = new BasicRadioButtonUI();
  35.       }
  36.  
  37.       return radioButtonUI;
  38.    }
  39.  
  40.    public Insets getDefaultMargin(AbstractButton b) {
  41.       return defaultMargin;
  42.    }
  43.  
  44.    public Dimension getPreferredSize(JComponent c) {
  45.       if (((Container)c).getComponentCount() > 0) {
  46.          return null;
  47.       } else {
  48.          AbstractButton b = (AbstractButton)c;
  49.          String text = b.getText();
  50.          Icon radioIcon = b.getIcon();
  51.          if (radioIcon == null) {
  52.             radioIcon = this.icon;
  53.          }
  54.  
  55.          Font font = ((Component)b).getFont();
  56.          FontMetrics fm = ((Component)b).getToolkit().getFontMetrics(font);
  57.          Rectangle iconR = new Rectangle();
  58.          Rectangle textR = new Rectangle();
  59.          Rectangle viewR = new Rectangle(32767, 32767);
  60.          SwingUtilities.layoutCompoundLabel(fm, text, radioIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewR, iconR, textR, text == null ? 0 : ((BasicToggleButtonUI)this).getDefaultTextIconGap(b));
  61.          Rectangle r = iconR.union(textR);
  62.          Insets insets = ((JComponent)b).getInsets();
  63.          r.width += insets.left + insets.right;
  64.          r.height += insets.top + insets.bottom;
  65.          return r.getSize();
  66.       }
  67.    }
  68.  
  69.    protected void installDefaults(JComponent c) {
  70.       super.installDefaults(c);
  71.       this.icon = this.createIcon();
  72.       LookAndFeel.installColorsAndFont(c, "RadioButton.background", "RadioButton.foreground", "RadioButton.font");
  73.    }
  74.  
  75.    public synchronized void paint(Graphics g, JComponent c) {
  76.       AbstractButton b = (AbstractButton)c;
  77.       this.model = b.getModel();
  78.       Dimension size = ((Component)c).getSize();
  79.       int var10000 = size.width;
  80.       var10000 = size.height;
  81.       Font f = ((Component)c).getFont();
  82.       g.setFont(f);
  83.       FontMetrics fm = g.getFontMetrics();
  84.       Rectangle viewRect = new Rectangle(size);
  85.       Rectangle iconRect = new Rectangle();
  86.       Rectangle textRect = new Rectangle();
  87.       Icon altIcon = b.getIcon();
  88.       String text = SwingUtilities.layoutCompoundLabel(fm, b.getText(), altIcon != null ? altIcon : this.icon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, ((BasicToggleButtonUI)this).getDefaultTextIconGap(b));
  89.       if (c.isOpaque()) {
  90.          g.setColor(((Component)b).getBackground());
  91.          g.fillRect(0, 0, size.width, size.height);
  92.       }
  93.  
  94.       if (altIcon != null) {
  95.          if (!this.model.isEnabled()) {
  96.             altIcon = b.getDisabledIcon();
  97.          } else if (this.model.isPressed() && this.model.isArmed()) {
  98.             altIcon = b.getPressedIcon();
  99.             if (altIcon == null) {
  100.                altIcon = b.getSelectedIcon();
  101.             }
  102.          } else if (this.model.isSelected()) {
  103.             altIcon = b.getSelectedIcon();
  104.          } else if (b.isRolloverEnabled() && this.model.isRollover()) {
  105.             altIcon = b.getRolloverIcon();
  106.          }
  107.  
  108.          if (altIcon == null) {
  109.             altIcon = b.getIcon();
  110.          }
  111.  
  112.          altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  113.       } else {
  114.          this.icon.paintIcon(c, g, iconRect.x, iconRect.y);
  115.       }
  116.  
  117.       if (text != null) {
  118.          if (this.model.isEnabled()) {
  119.             g.setColor(((Component)b).getForeground());
  120.             BasicGraphicsUtils.drawString(g, text, this.model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  121.          } else {
  122.             g.setColor(((Component)b).getBackground().brighter());
  123.             BasicGraphicsUtils.drawString(g, text, this.model.getMnemonic(), textRect.x + 1, textRect.y + fm.getAscent() + 1);
  124.             g.setColor(((Component)b).getBackground().darker());
  125.             BasicGraphicsUtils.drawString(g, text, this.model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  126.          }
  127.  
  128.          if (((JComponent)b).hasFocus() && b.isFocusPainted() && textRect.width > 0 && textRect.height > 0) {
  129.             this.paintFocus(g, textRect, size);
  130.          }
  131.       }
  132.  
  133.    }
  134.  
  135.    protected void paintFocus(Graphics g, Rectangle textRect, Dimension size) {
  136.    }
  137. }
  138.